/*
 * $Id: SecurityUtilities.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.security.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.*;

/**
 * The utility class which provides helper methods.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public final class SecurityUtilities {
    /**
     * The logger.
     */
	private static Logger logger;

    /**
     * The static initialization block.
     */
    static {
	    logger = LoggerFactory.getLogger(SecurityUtilities.class);
    }

    /**
     * The private constructor.
     */
    private SecurityUtilities() {
    }

    /**
     * Builds the granted uuthorities based on the given roles.
     *
     * @param roles The string values representing the user roles.
     *
     * @return The collection of granted authorities.
     */
    public static Collection<GrantedAuthority> buildAuthorities(List<String> roles) {
        // Create a list of grants for this user
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

        for (String role : roles) {
            logger.debug("Grant {} role", role);
            authorities.add(new SimpleGrantedAuthority(role));
        }

        // Return list of granted authorities
        return authorities;
    }

    /**
     * Returns the server and port from the given url.
     *
     * @param url The url.
     *
     * @return The server and port.
     */
    public static String getServerAndPort(String url) {
        int index, indexTo;

        // Checks for the scheme
        if ((index = url.indexOf("://")) > -1) {
            index += 3;
        } else {
            index = 0;
        }

        // Checks for the server and port
        if ((indexTo = url.indexOf("/", index)) == -1) {
            indexTo = url.length();
        }

        return url.substring(index, indexTo);
    }

    /**
     * Returns the path and query string from the given url.
     *
     * @param url The url.
     *
     * @return The path and query string.
     */
    public static String getPathAndQueryString(String url) {
        int index, indexTo;

        // Checks for the scheme
        if ((index = url.indexOf("://")) > -1) {
            index += 3;
        } else {
            index = 0;
        }

        // Checks for the path and query string
        if ((indexTo = url.indexOf("/", index)) == -1) {
            return null;
        }

        return url.substring(indexTo);
    }

    /**
     * Replaces the server and port in the given url.
     *
     * @param url The url.
     * @param serverAndPort The server and port.
     *
     * @return The url, where the server and port was been replaced.
     */
    public static String replaceServerAndPort(String url, String serverAndPort) {
        int index, indexTo;

        StringBuilder newUrl = new StringBuilder();

        // Checks for the scheme
        if ((index = url.indexOf("://")) > -1) {
            index += 3;
        } else {
            index = 0;
        }

        // Checks for the server and port
        if ((indexTo = url.indexOf("/", index)) == -1) {
            //indexTo = url.length();
        }

        // Appends the original url scheme
        if (index > 0) {
            newUrl.append(url.substring(0, index));
        }

        // Appends the given server and port
        newUrl.append(serverAndPort);

        // Appends the original url path and query string
        if (indexTo > -1) {
            newUrl.append(url.substring(indexTo));
        }

        return newUrl.toString();
    }
}
